<!--
 * @Author: your name
 * @Date: 2021-08-25 09:15:37
 * @LastEditTime: 2021-08-25 18:19:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \briup\5-javascript\day03\1-对象的定义.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //对象：个体的概念
        //一、字面量方法
        var obj1 = {};
        var obj2 = null;
        var user = 'zs';
        var obj3 = {
            number: 1001,
            price: 200.2,
            title: '连衣裙',
            user: user,
            products: {
                name: '笔记本',
                price: 300
            },
            manger: null,
            // function sayName() {
            //     console.log("sayName function");
            // },
            id: 100,
            sayName: function() {
                console.log("sayName function");
            }

        };
        console.log(obj1, obj2, obj3, typeof obj3);
        //二、构造函数  function Object(){}
        var person = new Object();
        console.log(person);
        obj3.id = 200; //重新赋值
        person.age = 16; //新增属性

        console.log(obj3.id, person.id); //属性的访问
        person.sayAge = function() {
            console.log("sayAge");
        };
        person.name = "lisi";
        obj3.id = "qqq";
        console.log(person.name);
        console.log(person.sayAge);
        person.sayAge(); //add()
        function add(a, b) {
            console.log(a + b);
            return a + b;
        }
        add(5, 5);
        person.plus = add; //将函数放到对象中
        //person.plus(1, 2);
        console.log(person.plus, typeof person.plus);
        person.plus2 = add(10, 20);
        console.log(person.plus2, typeof person.plus2);

        //对象的访问
        //一、点访问
        console.log(person.age, person.cart);
        //person.plus(1, 2); //函数的访问：函数的属性
        console.log(person.plus); //函数名称
        //二、[]访问
        //中括号应用场景：key中有特殊符号
        //完全遍历  增强版的for循环：for...in
        //语法：for(var 自定义变量名保存属性    in  对象/数组){}

        console.log(person['age'], person['cart']);
        person['first name'] = '张';
        var obj4 = {
            name: 'zs',
            'first name': 'zs',
            'age': 12
        };
        console.log(obj4['first name']);
        var n = "first name";
        console.log(obj4[n]);
        for (var key in person) {
            var value = person[key];
            console.log(key, value, person[key]);
        }
    </script>
</head>

<body>

</body>

</html>